using AutoMapper;
using B.S.DataBase.MyProject.Domain.SmartHealthcareZSJ;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.HospitalCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.HospitalCommandHandler
{
    /// <summary>
    /// 医院更新命令处理类
    /// </summary>
    public class HospitalUpdateCommandHandler : IRequestHandler<HospitalUpdateCommand, APIResult<object>>
    {
        /// <summary>
        /// AutoMapper实例
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// SqlSugar上下文
        /// </summary>
        private readonly SqlSugarContext sugar;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="sugar"></param>
        public HospitalUpdateCommandHandler(IMapper mapper, SqlSugarContext sugar)
        {
            this.mapper = mapper;
            this.sugar = sugar;
        }

        /// <summary>
        /// 更新医院信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(HospitalUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                // 开启事务
                sugar.db.Ado.BeginTran();

                // 查询医院信息
                var model = await sugar.db.Queryable<HospitalZ>()
                    .Where(x => x.HospitalId == request.HospitalId)
                    .FirstAsync();

                if (model == null)
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = "未找到对应的医院信息";
                    result.data = 0;
                    return result;
                }

                // 更新医院信息
                model.HospitalName = request.HospitalName;
                model.HospitalPicture = request.HospitalPicture;
                model.IsRecommended = request.IsRecommended;
                model.DoctorCount = request.DoctorCount;
                model.Departmentquantity = request.Departmentquantity;
                model.HospitalDepartments = request.HospitalDepartments;
                model.Location = request.Location;
                model.DetailedAddress = request.DetailedAddress;
                model.HospitalPhone = request.HospitalPhone;
                model.HospitalIntroduction = request.HospitalIntroduction;

                var r = await sugar.db.Updateable(model).ExecuteCommandAsync();
                if (r > 0)
                {
                    // 提交事务
                    sugar.db.Ado.CommitTran();
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = r;
                }
                else
                {
                    // 回滚事务
                    sugar.db.Ado.RollbackTran();
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }
            }
            catch (Exception ex)
            {
                // 发生异常时回滚事务
                sugar.db.Ado.RollbackTran();
                // 记录异常信息
                Console.WriteLine($"修改医院信息时发生异常: {ex.Message}");
                result.Code = APIEnum.修改失败;
                result.Msg = $"修改医院信息失败：{ex.Message}";
                result.data = 0;
            }
            return await Task.FromResult(result);
        }
    }
} 